package net.comm.common;

import java.util.*;
import java.lang.reflect.*;

public class Pub_Message
{



    /******************************************************************************/
    /*功能ID    ：replaceAll                                                       */
/*功能描述  ：按一定长度输出报头字段抬头, 不足部分补空格                      */
/*调用参数  ：AnsiString strInput:报头字段字符串                              */
/*            AnsiString strPostfix:后缀，默认为": "                          */
/*返回值    ：AnsiString:固定长度的报头字段抬头字符串                         */
/*编程人    ：                                                          */
/*调用函数  ：                                                                */
    /******************************************************************************/
    public static String replaceAll(String strSource, String strMatch,
                                    String strGoal)
    {
        int iIndex = -1;
        String strOutput = "";
        if (strSource==null) return "";
        if (strMatch==null) return "";
        if (strGoal==null) return "";
        while (true)
        {
            iIndex = strSource.indexOf(strMatch);
            if ( -1 == iIndex)
            {
                strOutput = strOutput + strSource;
                break;
            }
            else
            {
                strOutput = strOutput + strSource.substring(0, iIndex) +
                    strGoal;
                if (iIndex + strMatch.length() >= strSource.length())
                {
                    strSource = "";
                }
                else
                {
                    strSource = strSource.substring(iIndex + strMatch.length());
                }
            }
        }

        return strOutput;
    }
    
    //用于Servlet中定义页面的头部
    public static StringBuffer getHtmlHead(String title,String background,String[] jssrcs)
    {
       StringBuffer htmlhead=new StringBuffer();
       htmlhead.append("<html>\n<head>\n");
       htmlhead.append("<%@ page language=\"java\" contentType=\"text/html; charset=UTF-8\" pageEncoding=\"UTF-8\"%>");
       htmlhead.append("<meta http-equiv=\"Content-Type\"  content=\"text/html; charset=UTF-8\">");
       htmlhead.append("<script src=\"../../js/common.js\"></script>");
       htmlhead.append("<title>");
       htmlhead.append(title);
       htmlhead.append("</title>");
       if (jssrcs!=null)
       {
          for (int i=0;i<jssrcs.length;i++)
          {
             htmlhead.append("<script src=\"");
             htmlhead.append(jssrcs[i]);
             htmlhead.append("\"></script>");
          }
       }
       htmlhead.append("</head>");
       htmlhead.append("<body background=\"");
       htmlhead.append(background);
       htmlhead.append("\">");
       return htmlhead;
    }
 
    //用于Servlet中定义XML页面的头部
    public static StringBuffer getXMLHead(String title,String background,String[] jssrcs)
    {
       StringBuffer htmlhead=new StringBuffer();
       htmlhead.append("<html>\n<head>\n");
       htmlhead.append("<%@ page language=\"java\" contentType=\"text/html; charset=UTF-8\" pageEncoding=\"UTF-8\"%>");
       htmlhead.append("<meta http-equiv=\"Content-Type\"  content=\"text/html; charset=UTF-8\">");
       htmlhead.append("<script src=\"../../js/common.js\"></script>");
       htmlhead.append("<title>");
       htmlhead.append(title);
       htmlhead.append("</title>");
       if (jssrcs!=null)
       {
          for (int i=0;i<jssrcs.length;i++)
          {
             htmlhead.append("<script src=\"");
             htmlhead.append(jssrcs[i]);
             htmlhead.append("\"></script>");
          }
       }
       htmlhead.append("</head>");
       htmlhead.append("<body background=\"");
       htmlhead.append(background);
       htmlhead.append("\">");
       return htmlhead;
    }
    
	public static String getOrderString(Object obj,String cols,String opr){

		StringBuffer buf = new StringBuffer();
		//因MYSQL修改，分隔符使用空格
		StringTokenizer st = new StringTokenizer(cols,",");
		if (st.countTokens()==0)
		{
			System.out.println("传入order by 字段串["+cols+"]无效");
			return null;
		}
		String[] column = new String[st.countTokens()];
		String[] coldata = new String[st.countTokens()];
		
		int j=0;
		Field field;
		while (st.hasMoreTokens())
		{
			column[j++]=st.nextToken();
			try{
			field = obj.getClass().getDeclaredField(column[j-1]);
			if (field.getType().getName().equals("double"))
				coldata[j-1]=String.valueOf(field.getDouble(obj));
			else if (field.getType().getName().equals("int"))
				coldata[j-1]=String.valueOf(field.getInt(obj));
			else
			{
				coldata[j-1]="'"+(String)field.get(obj)+"'";
			}
			}catch (Exception e)
			{
				return null;
			}
		}
		int flag=0;
		buf.append(" and (");
		for (int i=0;i<column.length;i++)
		{
			flag=0;
			for (int k=0;k<i;k++)
			{
				System.out.println(column[k]);
				if (k==0 && flag==0)
				{
//					buf.append("("+column[k]+"="+coldata[k]);
					buf.append(column[k]+"="+coldata[k]);
					flag=1;
				}
				else
					buf.append(" and "+column[k]+"="+coldata[k]);
			}
			if (i==column.length-1)
				opr=opr+"=";
			if (flag==0)
			{
//				buf.append("("+column[i]+opr+coldata[i]);
				buf.append(column[i]+opr+coldata[i]);
				flag=1;
			}
			else
				buf.append(" and "+column[i]+opr+coldata[i]);
//			buf.append(") or ");
			buf.append(" or ");
		}
		buf.delete(buf.length()-4,buf.length());
		buf.append(")");
		return (buf.toString());
	}
}